package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;

import Model.Order;
import Model.User;
import utils.DBHelper;

public class UserDao {
    Connection conn;
    String sql;
    PreparedStatement ps;
    ResultSet rs;
    public UserDao() {
    	conn = DBHelper.getConnection();
    }
    public ArrayList<User> getAll(){
    	ArrayList<User> list = new ArrayList<User>();
    	sql = "select * from tb_user";
    	try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				int user_id = rs.getInt("user_id");
				String username = rs.getString("username");
				String userpass = rs.getString("userpass");
				String mobilenum = rs.getString("mobilenum");
				String address = rs.getString("address");
				String comment = rs.getString("comment");
				User user = new User(user_id,username,userpass,mobilenum,address,comment);
				list.add(user);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return list;
    }
    public boolean insert(User user) {
    	sql = "insert into tb_user(username,userpass,mobilenum,address,comment) values(?,?,?,?,?)";
    	try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getUserpass());
			ps.setString(3, user.getMobilenum());
			ps.setString(4, user.getAddress());
			ps.setString(5, user.getComment());
			int k = ps.executeUpdate();
			if(k>0)return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return false;
    	
    	
    }
    public User getUserById(int id) {
    	sql = "select * from tb_user where user_id=?";
    	try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if(rs.next()) {
				String username = rs.getString("username");
				String userpass = rs.getString("userpass");
				String mobilenum = rs.getString("mobilenum");
				String address = rs.getString("address");
				String comment = rs.getString("comment");
				User user = new User(id,username,userpass,mobilenum,address,comment);
				return user;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return null;
    }
    public boolean update(User user) {
    	sql = "update tb_user set username=?,userpass=?,mobilenum=?,address=?,comment=? where user_id=?";
    	try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getUserpass());
			ps.setString(3, user.getMobilenum());
			ps.setString(4, user.getAddress());
			ps.setString(5, user.getComment());
			ps.setInt(6, user.getUser_id());
			int k = ps.executeUpdate();
			if(k>0)return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return false;
    }
    public boolean delete(int id) {
    	sql = "delete from tb_user where user_id=?";
    	try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			int k = ps.executeUpdate();
			if(k>0)return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return false;
    }
    public ArrayList<User> getUserByCondition(String condition,String value){
    	ArrayList<User> list = new ArrayList<User>();
    	sql = "select * from tb_user where "+condition+" like "+"'%"+value+"%'";
    	System.out.println("sql:"+sql);
    	try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				int user_id = rs.getInt("user_id");
				String username = rs.getString("username");
				String userpass = rs.getString("userpass");
				String mobilenum = rs.getString("mobilenum");
				String address = rs.getString("address");
				String comment = rs.getString("comment");
				User user = new User(user_id,username,userpass,mobilenum,address,comment);
				list.add(user);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return list;
    }
    public boolean updateUserByUserId(User user) {
    	sql = "update tb_user set username=?,userpass=?,mobilenum=?,address=? where user_id=?";
    	try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getUserpass());
			ps.setString(3, user.getMobilenum());
			ps.setString(4, user.getAddress());
			//ps.setString(5, user.getComment());
			ps.setInt(5, user.getUser_id());
			int k = ps.executeUpdate();
			if(k>0)return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return false;
    }
   
    // �ж��Ƿ����ָ�����û���������
    public User login(String username,String password) {
    	sql = "select * from tb_user where username=? and userpass=?";
    	try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if(rs.next()) {
				User user = new User();
				int id = rs.getInt("user_id");
				user.setUser_id(id);
				return user;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return null;
    	
    }
    
    public boolean register(String username,String userpass) {
    	if(isExist(username)) {
    		return false;
    	}
    	
    	sql = "insert into tb_user(username,userpass,mobilenum) values(?,?,'')";
    	
    	try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, userpass);
			int isOk = ps.executeUpdate();
			System.out.println("注册结果：" + (isOk > 0));
			return isOk > 0;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}   	
    }
    
    public boolean updateAdvatar(String fileUrl,int user_id) {
    	sql = "update tb_user set avatar = ? where user_id = ?";
    	boolean isOk = false;
    	try {
    		ps = conn.prepareStatement(sql);
			ps.setString(1, fileUrl);
			ps.setInt(2, user_id);
			int k = ps.executeUpdate();
			if(k > 0) {
				return true;
			}
    		return false;
    	}catch(Exception e) {
    		e.printStackTrace();
			return false;
    	}
    }
    
    public String getUserAdvById(int user_id) {
    	sql = "select avatar from tb_user where user_id = ? ";
    	try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, user_id);
			rs = ps.executeQuery();
			if(rs.next()) {
				String url = rs.getString("avatar");
				
				return url;
			
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return "";
    }
    
    //检查账号是否存在
    public boolean isExist(String username) {
    	sql = "select user_id from tb_user where username = ?";
    	boolean isOk = false;
    	try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			if (rs.next()) {
				System.out.println("用户id:" + username + "存在");
		       return true;
		    }
			
			return false;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
    	
    	
    }
    
    //检查账号是否存在
    public boolean isExist(int user_id) {
    	sql = "select user_id from tb_user where user_id = ?";
    	boolean isOk = false;
    	try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, user_id);
			rs = ps.executeQuery();
			if (rs.next()) {
				System.out.println("账号:" + user_id + "已经存在");
		       return true;
		    }
			
			return false;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
    	
    	
    }
    
    
    
}
